package com.tjl.foodapp.utils;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.annotation.NonNull;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.GsonBuilder;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.CenterPopupView;
import com.tjl.foodapp.MainActivity;
import com.tjl.foodapp.MeFragment;
import com.tjl.foodapp.R;
import com.tjl.foodapp.User;
import com.tjl.foodapp.Utils;

public class SettlePopup extends CenterPopupView {

            private static RequestQueue queue;
            private static User user;
            EditText settle_address;
            SubmitHandler submitHandler;
            public interface SubmitHandler{
                void onSubmit(String time);
            }

            public SettlePopup(@NonNull Context context,SubmitHandler submitHandler) {
                super(context);
                this.submitHandler = submitHandler;
            }
            // 返回自定义弹窗的布局
            @Override
            protected int getImplLayoutId() {
                return R.layout.settle_popup;
            }
            // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
            @Override
            protected void onCreate() {
                super.onCreate();
                queue = Volley.newRequestQueue(getContext());
                getUserByURL(Utils.urlBase+"and_getUserByUserId.do?id="+MainActivity.user_id);

                Spinner spinner = findViewById(R.id.settle_time);
                settle_address = findViewById(R.id.settle_address);
                Button settleBtn = findViewById(R.id.settleBtn);
                //生成时间
                TimeUtil.generateTimeOptions(spinner);
                //提交逻辑
                settleBtn.setOnClickListener(v -> {
                    //获取地址
                    String address = settle_address.getText().toString();
                    //获取时间
                    String time = spinner.getSelectedItem().toString();
                    //更新用户地址 地址修改的情况下
                    if (!user.getAddress().equals(address)){
                        user.setAddress(address);
                        MeFragment.updateUserByURL(Utils.urlBase+"and_updateUserByUserId.do",user,queue);
                    }
                    submitHandler.onSubmit(time);
                });

            }
            // 设置最大宽度，看需要而定，
            @Override
            protected int getMaxWidth() {
                return super.getMaxWidth();
            }
            // 设置最大高度，看需要而定
            @Override
            protected int getMaxHeight() {
                return super.getMaxHeight();
            }
            // 设置自定义动画器，看需要而定
            @Override
            protected PopupAnimator getPopupAnimator() {
                return super.getPopupAnimator();
            }
            /**
              * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
              *
              * @return
              */
             @Override
             protected int getPopupWidth() {
                 return 1000;
             }

             /**
              * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
              *
              * @return
              */
             @Override
             protected int getPopupHeight() {
                 return 400;
             }

    public void getUserByURL(String url){
        Response.Listener listener1 = new Response.Listener() {
            @Override
            public void onResponse(Object o) {
                user = new GsonBuilder().setDateFormat("MMM dd, yyyy, hh:mm:ss a").create().fromJson(o.toString(),User.class);
                //设置地址 获取当前用户的地址
                settle_address.setText(user.getAddress());
            }
        };
        Response.ErrorListener listener2 = new Response.ErrorListener(){

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();
            }
        };
        JsonRequest request = new JsonObjectRequest(url,null,listener1,listener2);
        queue.add(request);
    }
}